import 'package:flutter/material.dart';
import 'package:hospitable_rental/utils/config.dart';
import 'package:hospitable_rental/widgets/common_check_button.dart';

class RoomApplicanceItem {
  final String title;
  final int iconPoint;
  final bool isChecked;

  const RoomApplicanceItem(
    this.title,
    this.iconPoint,
    this.isChecked,
  );
}

const List<RoomApplicanceItem> _applianceItemList = [
  RoomApplicanceItem("衣柜", 0xe918, false),
  RoomApplicanceItem("洗衣机", 0xe917, false),
  RoomApplicanceItem("空调", 0xe90d, false),
  RoomApplicanceItem("天然气", 0xe90f, false),
  RoomApplicanceItem("冰箱", 0xe907, false),
  RoomApplicanceItem("暖气", 0xe910, false),
  RoomApplicanceItem("电视", 0xe908, false),
  RoomApplicanceItem("热水器", 0xe912, false),
  RoomApplicanceItem("宽带", 0xe90e, false),
  RoomApplicanceItem("沙发", 0xe913, false),
];

class RoomApplicance extends StatefulWidget {
  final ValueChanged<List<RoomApplicanceItem>>? onChanged;

  const RoomApplicance({Key? key, this.onChanged}) : super(key: key);

  @override
  _RoomApplicanceState createState() => _RoomApplicanceState();
}

class _RoomApplicanceState extends State<RoomApplicance> {
  List<RoomApplicanceItem> _list = _applianceItemList;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Wrap(
        runSpacing: 20.0,
        children: _list
            .map(
              (item) => GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: () {
                  setState(() {
                    _list = _list
                        .map(
                          (e) => e == item
                              ? RoomApplicanceItem(
                                  item.title,
                                  item.iconPoint,
                                  !item.isChecked,
                                )
                              : e,
                        )
                        .toList();
                    widget.onChanged!(_list);
                  });
                },
                child: Container(
                  width: MediaQuery.of(context).size.width / 5,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        IconData(item.iconPoint, fontFamily: Config.CommonIcons),
                        size: 40.0,
                        color: Colors.grey,
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(item.title),
                      ),
                      CommonCheckButton(item.isChecked),
                    ],
                  ),
                ),
              ),
            )
            .toList(),
      ),
    );
  }
}

class RoomApplicanceList extends StatelessWidget {
  final List<String> list;

  const RoomApplicanceList({
    Key? key,
    required this.list,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var showList = _applianceItemList
        .where(
          (item) => list.contains(item.title),
        )
        .toList();
    return showList.length == 0
        ? Container(
            alignment: Alignment.center,
            child: Text("暂无房屋配置信息"),
          )
        : Container(
            child: Wrap(
              runSpacing: 20.0,
              children: showList
                  .map(
                    (item) => GestureDetector(
                      child: Container(
                        width: MediaQuery.of(context).size.width / 5,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(
                              IconData(item.iconPoint, fontFamily: Config.CommonIcons),
                              size: 40.0,
                              color: Colors.grey,
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Text(item.title),
                            ),
                          ],
                        ),
                      ),
                    ),
                  )
                  .toList(),
            ),
          );
  }
}
